using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNALibrary
{
    /// <summary>
    ///  Class for a first-person Camera (attached to an avatar)
    /// </summary>
    public class FPCamera : Camera
    {
        #region Fields and properties

        //Vector3 cameraFPPosition;   // in avatar object space
        Vector3 cameraFPUpVector;   // in avatar object space 
        Vector3 cameraFPHeading;    // in avatar object space

        Vector3 avatarPosition;     // in world space
        Matrix  avatarOrientation;  // in world space 
        

        #endregion Fields and properties

        public FPCamera(GraphicsDevice g, Vector3 pos, Vector3 heading, Vector3 up, Vector3 apos)
            : base(g)
        {
            // Initially : avatar object space = world space
            position = pos;
            Vector3.Normalize(ref heading, out cameraFPHeading);
            Vector3.Normalize(ref up, out cameraFPUpVector);

            avatarPosition = apos;  // initial avatar position in world space
            avatarOrientation = Matrix.Identity;

            ResetCamera();

        }

        public void SetFPCamera (Vector3 apos, Matrix orientation)
        {
            avatarPosition = apos;
            avatarOrientation = orientation;

            ResetCamera();
        }

        public void ResetCamera()
        {
            Vector3.TransformNormal(ref cameraFPUpVector, ref avatarOrientation, out upVector);
            Vector3.TransformNormal(ref cameraFPHeading, ref avatarOrientation, out aimDirection);

            target = position + aimDirection;

            viewChanged = true;

        }
    }
}
